Basic libraries for dealing with data:
library(plotly) # interactive graphs
library(RColorBrewer) # make new color sets
library(tidyverse) # easy manipulation of dataframes
library(modelr) # easy piping stats/model functions
library(DT) # generate interactive html tables
library(rvest) # scrap web pages
source("utils.R") # custom files with utilitary functions
Some libraries for better handling Rmarkdown:
library(DT) # iteractive datatables
despesas.funcao.2017 <- read_csv2(unz(fin.estado17.path, "finbra.csv"),
locale=locale(encoding="ISO-8859-15"),
skip=3,
col_types = cols(.default = col_factor(NULL),
"População" = col_number(),
Valor = col_number()))
datatable(despesas.funcao.2017, options=list(pageLength=5), filter="top")
datatable(estados45ano.rawtable, options=list(scrollX=T), rownames=F)
# leitura do CSV extraído do HTML
estados45ano.df <- read_csv2(join.path(data.dir, "ideb/ideb-csv/ideb-estados-45ano.csv"),
col_types = cols(Estado = col_factor(NULL),
Ano = col_factor(NULL),
Ideb = col_double(),
Meta = col_double()))
estados45ano.df <- mutate(estados45ano.df, Estado=rename.brstates(Estado))
datatable(estados45ano.df, options=list(scrollX=T), rownames=F, filter="top")
datatable(despesas.funcao.2017,
rownames=F,
filter="top",
options=list(pageLength = 5, scrollX=T))
despesas.funcao.2017 %>%
select(Conta) %>%
unique() %>%
datatable(rownames=F, filter="top")
despesas.funcao.2017 %>%
select(UF, População) %>%
unique() %>%
ggplot(aes(x=UF, y=População)) + geom_bar(stat = "identity")
É aplicado um filtro para calcular a soma total de investimentos em educação de todo tipo “código inicia com 12”
Problema: nem todos os estados têm lançamentos para “Educação Básica” e alguns não têm para “Ensino Fundamental”, então vou deixar todos com código “12” por enquanto.
# prepara tabela com "UF", "População", "Investimentos" e "Proporção"
despesas.educ.2017 <- despesas.funcao.2017 %>%
filter(startsWith(as.character(Conta), "12")) %>% # todas que começam com "12", "12.1234" etc.
## filter(startsWith(as.character(Conta), "12.361")) %>% # Ensino Fundamental
group_by(UF) %>%
summarise(População=first(População), InvEduc = sum(Valor)) %>%
mutate(PropInv = InvEduc/População)
# Gráfico de barra Investimento total
ggplot(despesas.educ.2017, aes(x=UF, y=InvEduc)) +
geom_bar(stat = "identity") +
ggtitle("Despesas em Educação por Estado") +
theme(plot.title = element_text(hjust = 0.5)) +
ylab("Investimento Total")
# Gráfico de barra com proporções Investimento/População
ggplot(despesas.educ.2017, aes(x=UF, y=PropInv)) +
geom_bar(stat="identity") +
geom_smooth() +
ggtitle("Investimento em Educação Per Capta") +
ylab("Investimento em Educação Per Capta")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
estados45ano.df %>%
filter(Ano==2017) %>%
mutate(Estado = factor(Estado)) %>%
ggplot(aes(x=Estado, y=Ideb)) + geom_bar(stat="identity")
# prepara tabela, unindo Ideb com Proporção de investimentos
estados45ano.df %>%
filter(Ano==2017) %>%
rename(UF=Estado) %>%
inner_join(despesas.educ.2017, by="UF") %>%
mutate(UF=factor(UF)) ->
estadosInv.2017
## Warning: Column `UF` joining factors with different levels, coercing to character vector
p-value <2e-16ks.test(estadosInv.2017$InvEduc, 'pnorm')
##
## One-sample Kolmogorov-Smirnov test
##
## data: estadosInv.2017$InvEduc
## D = 1, p-value <2e-16
## alternative hypothesis: two-sided
ks.test(estadosInv.2017$Ideb, 'pnorm')
## Warning in ks.test(estadosInv.2017$Ideb, "pnorm"): ties should not be present for the
## Kolmogorov-Smirnov test
##
## One-sample Kolmogorov-Smirnov test
##
## data: estadosInv.2017$Ideb
## D = 1, p-value <2e-16
## alternative hypothesis: two-sided
p-value <2e-16shapiro.test(estadosInv.2017$InvEduc)
##
## Shapiro-Wilk normality test
##
## data: estadosInv.2017$InvEduc
## W = 0.54, p-value = 4e-08
p-value = 0.2shapiro.test(estadosInv.2017$Ideb)
##
## Shapiro-Wilk normality test
##
## data: estadosInv.2017$Ideb
## W = 0.95, p-value = 0.2
ggplot(estadosInv.2017) +
geom_point(aes(x=InvEduc, y=Ideb)) +
xlab("Investimento per Capta") +
ylab("IDEB")
cor.test( ~ InvEduc + Ideb,
data=estadosInv.2017,
method = "spearman",
continuity = FALSE,
conf.level = 0.95)
## Warning in cor.test.default(x = c(6146589064.27, 4005264736.14, 5036294136.68, : Cannot compute
## exact p-value with ties
##
## Spearman's rank correlation rho
##
## data: InvEduc and Ideb
## S = 1700, p-value = 0.01
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.4789
cor.test( ~ InvEduc + Ideb,
data=estadosInv.2017,
method = "pearson",
continuity = FALSE,
conf.level = 0.95)
##
## Pearson's product-moment correlation
##
## data: InvEduc and Ideb
## t = 3, df = 25, p-value = 0.007
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.1614 0.7456
## sample estimates:
## cor
## 0.5101